package com.ericxu.weixin.msg;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.dom4j.Document;

public class TextWeiXinMessage extends WeiXinMessage {
	private static final String FORMAT = "<xml>"
			+ "<ToUserName><![CDATA[%s]]></ToUserName>"
			+ "<FromUserName><![CDATA[%s]]></FromUserName>"
			+ "<CreateTime>%s</CreateTime>"
			+ "<MsgType><![CDATA[text]]></MsgType>"
			+ "<Content><![CDATA[%s]]></Content>"
			+ "<MsgId>%s</MsgId>"
			+ "</xml>";
	private String _content;

	public TextWeiXinMessage(Document doc) {
		super(doc);
		this._content = doc.selectSingleNode("//Content").getText();
	}
	
	public void setContent(String content) {
		this._content = content;
	}
	
	public String getContent() {
		return this._content;
	}

	@Override
	public WeiXinMessage excute() {
		String frmUser = this.frmUserName;
		this.frmUserName = this.toUserName;
		this.toUserName = frmUser;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		this._content = df.format(new Date());
		return this;
	}

	@Override
	public String toString() {
		return String.format(FORMAT, this.toUserName, this.frmUserName, this.createTime, 
				this._content, this.msgId);
	}
}
